package com.zj.esproxy.wrapper;

import lombok.Data;

import java.util.ArrayList;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author arthur_zhou
 * @date 2023/11/7 14:17
 */
@Data
public class AggNestWrapper {

    private final AggWrapper aggWrapper;

    public AggNestWrapper(AggWrapper aggWrapper) {
        this.aggWrapper = aggWrapper;
    }

    /**
     * 嵌套聚合
     *
     * @param consumer 函数
     * @return 自己
     */
    public AggNestWrapper agg(Consumer<AggWrapper> consumer) {
        AggWrapper aggWrapper = new AggWrapper();
        consumer.accept(aggWrapper);
        return agg(aggWrapper);
    }

    /**
     * 嵌套聚合
     *
     * @param wrapper 拼接器
     * @return 自己
     */
    public AggNestWrapper agg(AggWrapper wrapper) {
        if (Objects.nonNull(wrapper) && Objects.nonNull(wrapper.getAggType())) {
            if (Objects.isNull(this.aggWrapper.getAggs())) {
                this.aggWrapper.setAggs(new ArrayList<>());
            }
            this.aggWrapper.getAggs().add(wrapper);
        }
        return this;
    }
}
